/*
 * EquilibriumProblem.h
 *
 *  Created on: 27 Jul 2011
 *      Author: Allan
 */

#ifndef EQUILIBRIUMPROBLEM_H_
#define EQUILIBRIUMPROBLEM_H_

// C++ includes
#include <vector>
using namespace std;

// Eigen includes
#include <Eigen/Core>
using namespace Eigen;

// GeoReact includes
#include "EquilibriumConditions.h"
#include "Multiphase.h"
#include "ReactionSystem.h"

#include "Numerics/MultirootSolver.h"
#include "Utils/Types.h"

// GeoReact forward declarations
class EquilibriumSolver;

class EquilibriumProblem : public MultirootProblem
{
public:
	EquilibriumProblem();
	
	EquilibriumProblem(const Multiphase& multiphase, const ReactionSystem& eReactions);
	
	virtual	~EquilibriumProblem();
	
	void Initialiaze(const Multiphase& multiphase, const ReactionSystem& eReactions);
	
	void SetEquilibriumConditions(const vector<EquilibriumCondition>& eConditions);
	
	void SetParameters(double T, double P);
	
	virtual void Function(const VectorXd& n, VectorXd& F);
	
	virtual void Jacobian(const VectorXd& n, MatrixXd& J);
	
	friend class EquilibriumSolver;
	
private:
	/// The multiphase object
	Multiphase multiphase;
	
	/// The system of equilibrium-controlled reactions
	ReactionSystem eReactions;
	
	/// The number of species (Ni), of equilibrium reactions (Ne) and of equilibrium conditions (Nj)
	unsigned Ni, Ne, Nj;
	
	/// The stoichiometric matrix of the system of reactions
	MatrixXd uei;
	
	/// The matrices that define the set of equilibrium constraints in the system 
	MatrixXd alpha;
	
	/// The functions that evaluate the right-hand side vector h of the system of equilibrium-constraint equations
	vector<FunctionTPna> hfunctions;
	
	/// The temperature (T) and pressure (P)
	double T, P;
	
	/// The activities (a) of all species
	VectorXd a;
	
	/// The equilibrium constants (Ke) of the equilibrium-controlled reactions
	VectorXd Ke;
	
	/// The right-hand side vector h of the equilibrium condition equations
	VectorXd h;
};

#endif /* EQUILIBRIUMPROBLEM_H_ */
